package cn.tedu.egweibo3.controller;

import cn.tedu.egweibo3.conmon.response.JsonResult;
import cn.tedu.egweibo3.mapper.CommentMapper;
import cn.tedu.egweibo3.pojo.dto.CommentDTO;
import cn.tedu.egweibo3.pojo.entity.Comment;
import cn.tedu.egweibo3.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Api(tags = "评论模块")
@RestController
@RequestMapping("/v1/comments/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @ApiOperation("发布评论")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody @Validated CommentDTO commentDTO,@ApiIgnore HttpSession session) {
        log.error("commentDTO"+commentDTO);
        UserVO userVO=(UserVO)session.getAttribute("user");
        Comment comment = new Comment();
        comment.setContent(commentDTO.getContent());
        comment.setWeiboId(commentDTO.getWeiboId());
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());
        commentMapper.insertComment(comment);
        return JsonResult.ok();
    }

    @ApiOperation("获取评论列表")
    @ApiImplicitParam(name = "id",value = "微博id",required = true,dataType = "Long")
    @GetMapping("selectByWeiboId")
    public JsonResult selectByWeiboId(Long id) {
        return JsonResult.ok(commentMapper.selectByWeiboId(id));
    }
}





























